<#include "custom.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>
$(document).ready(function() {

	// 选择状态
	$('#typeOption,#statusOption').on('touchend click', 'li', function() {
		$(this).parent().attr('value', $(this).attr('value'));
	});

	// 查询
	$('#search').on('touchend click', function() {
		params.type = $('#typeOption').attr('value');
		params.status = $('#statusOption').attr('value');
		params.keyword = $('#keyword').val();
		params.currpage = 1;
		removeCache('${classNameLower}_page');
		getProductionList();
	});
	
	$('#addNotice').on('touchend click', function() {
		setCache('${classNameLower}_add_id', null);
	});
	var params = {
			currpage : 1
	};
	
	getProductionList();

	$('#messageSend').on('touchend click', getProductionList);
	
	function getProductionList(){
		$.post('../../${classNameLower}/list.do', params, function(data) {
			if (data.status == 0){
				// 移除表格中所有项
				$('#table tr.dataItem').remove();
				// 添加每一项
				for (var i = 0, len = data.message.length; i < len; i++) {
					appendRecord(data.message[i]);
				}
				// 显示分页
				$('#pager').empty().append($(data.pager));
			} else if (data.status == 1) {
				alert(data.error);
			} else if (data.status == 2) {
			} else if (data.status == 3) {
				alert("系统异常");
			}
		}, 'json');
	}
	
	function appendRecord(obj) {
		var deleteStr = '<td  type="check" chootype="chooOneBtn" class="cp " check="0">'
			+ '<img class="imgs" width="20" src="../../resource/images/Radio_buttons2.png" alt="" />'
			+ '</td>';
		
		operateStr = '<a href="${classNameLower}_add.html" class="c2">编辑</a>&nbsp;&nbsp;<a href="javascript:void(0);" class="c2">删除</a>';
		
		var picturePath = "";
		if(obj.minPicPath){
			var picturePath = ftpUrl + obj.minPicPath;
		}
		
		var html = '<tr id="'+obj.id+'" class="dataItem">'
			//+ deleteStr
			+ '<td><a class="c1" href="${classNameLower}_details.html" name="messageDetail">'
			+ 	'<p>' + obj.name + '</p><img width="140" height="80" alt="" src="' +  picturePath + '"></a></td>'
			+ '<td class="hidden-s hidden-m">'+obj.sort+'</td>'
			+ '<td class="hidden-s hidden-m">'+obj.createTime+'</td>'
			+ '<td>'+operateStr+'</td>'
			+ '</tr>';
		
		var tr = $("#table tr").last().before($(html).data('obj', obj));

		// 绑定事件
		tr.prev().find('a').each(function(index, element) {
			if ($(this).text() == '编辑') {
				$(element).on('touchend click', function() {
					var obj = $(this).parent().parent().data('obj');
					setCache('${classNameLower}_add_id', obj);
				});
			}
			if ($(this).text() == '删除') {
				$(element).on('touchend click', function() {
					deleteProduction(obj.id);
				});
			}
			if ($(this).attr('name') == 'messageDetail') {
				$(element).on('touchend click', function() {
					setCache('message_for_detail', obj);
				});
			}
		});

		//checkSw($('#table [chootype=chooAllBtn]'), '1', '0');
	}
	
	function deleteProduction(id) {
		if(id){
			popUpWindow({
				popType : 1, // 1：确定框  2：提示框框  3：需确认的提示框
				title : "确定删除么？",
				callback : function(){
					//确定删除后操作
					$.post('../../${classNameLower}/delete.do', {'ids':id}, function(data) {
						if (data.status == 0){
							getProductionList();
						} else if (data.status == 1) {
						} else if (data.status == 2) {
						} else if (data.status == 3) {
							popUpWindow({title:"系统异常",popType:2});
						}
					}, 'json');
				}
			});
			//if(confirm('确定删除吗?')){
            //
			//}
		}else{
			alert("不能获取到产品id！");
		}
	}
	
	$('#delete').unbind('touchend click');
	$('#delete').on('touchend click', function() {
		var selectItems = [];
		var deleteItems = [];

		$('#table [chootype=chooOneBtn]').each(function() {
			if ($(this).attr('check') == 1) {
				deleteItems.push($(this).parent());
				selectItems.push($(this).parent().attr('id'));
			}
		});
		
		if (selectItems.length == 0) {
			alert('请选择要删除项！');
			return;
		}
		
		if (!confirm('确认删除吗？')) {
			return;
		}
		selectItems.length == 0 || (function() {
			$.post('../../${classNameLower}/delete.do', {ids : selectItems}, function(data) {
				if (data.status == 0){
					for (var i = 0, len = deleteItems.length; i < len; i++) {
						deleteItems[i].removeData('obj');
						deleteItems[i].remove();
					}
					getProductionList();
				} else if (data.status == 1) {
				} else if (data.status == 2) {
				} else if (data.status == 3) {
					alert("系统异常");
				}
			}, 'json');
		})();
	});
	
	window.changepage = function(page) {
		params.currpage = page;
		getProductionList();
	};

	
});
